import Vue from "vue"
import Vuex from "vuex"
import { getItem, setItem } from "@/utils/stroage"
import request from "@/utils/request"
Vue.use(Vuex)
const usercookies = "user-key"
const PersonAccountKey = "Person-account-key"
const currentMusic = "current-music"
const currentMusicUrl = "current-music-url"
const currentIndex = "current-Index"
const playmusicList = "play-music-List"
const playlistIDs = "play-list-IDs"

// 获取某些音乐特定详情
// const getmusicdetailAPI = (ids) => {
//   let cookie = store.state.usercookie
//   return request({
//     method: "GET",
//     url: "/song/detail",
//     params: {
//       ids,
//       cookie
//     },
//   })
// }
/* 
获取音乐url
 */
export const getmusicurlAPI = (id) => {
  let cookie = store.state.usercookie
  return request({
    method: "GET",
    url: "/song/url",
    params: {
      id,
      cookie,
    },
  })
}
const moduleA = {
  state: {
    isHaveModule: false,
    getPersonAccount: getItem(PersonAccountKey),
  },
  mutations: {
    getdatabyA(state, payload, getter) {
      console.log(state, payload, getter)
    },
  },
  actions: {
    getdatabyB({ state, payload, rootState }, getter) {
      console.log(state, payload, getter, rootState)
    },
  },
  getters: {
    getdatabyAget(state, getter, root) {
      return console.log(state, getter, root)
    },
  },
}

const store = new Vuex.Store({
  state: {
    isLogin: false,
    usercookie: getItem(usercookies),
    PersonAccount: getItem(PersonAccountKey),
    UserInfo: {},
    // 音乐播放列表
    playmusicList: getItem(playmusicList) ? getItem(playmusicList) : [],
    // 当前歌单的音乐
    currentSongs: [],
    // 添加过的歌单id，用于辨识歌单添加
    playlistIDs: getItem(playlistIDs) ? getItem(playlistIDs) : [],
    //当前播放的音乐
    currentMusic: getItem(currentMusic),
    // 当前音乐url
    currentMusicUrl: getItem(currentMusicUrl),
    // 当前音乐位于列表位置
    currentIndex: getItem(currentIndex),
    // 播放按钮的状态
    musicisPlay: false,
    // 当前歌曲时间进度
    currentTimesecond: 0,
    // 真实ip
    realIP: "",
  },
  mutations: {
    // 将用户cookie本地化存储
    setUser(state, data) {
      state.usercookie = data

      setItem(usercookies, state.usercookie)
    },
    // 保存用户信息
    setPersonAccount(state, data) {
      state.PersonAccount = data

      setItem(PersonAccountKey, state.PersonAccount)
    },
    // 设置用户信息
    setUserInfo(state, data) {
      state.UserInfo = data
    },
    // 添加当前歌单歌曲进去音乐列表
    addMusictoMusiclist(state, data) {
      if (state.currentSongs.length != 0) {
        // 判断歌单是否添加过
        let isHave = state.playlistIDs.findIndex((ele) => {
          return ele == data
        })
        if (isHave === -1) {
          state.playmusicList = [...state.playmusicList, ...state.currentSongs]
          state.playlistIDs.push(data)
          console.log(state.playlistIDs)
          //  若当前没有播放的音乐，自动播放第一首歌曲
          if (state.currentIndex == null || state.currentIndex == -1) {
            this.dispatch("getMusicDetail", state.playmusicList[0])
          }
          setItem(playlistIDs, state.playlistIDs)
          setItem(playmusicList, state.playmusicList)
        } else if (isHave === 0) {
          console.log("已添加当前歌单")
        }
      } else if (state.currentSongs.length == 0) {
        console.log("数组还没加载")
      }
    },
    // 当前歌曲列表
    setcurrentSongs(state, data) {
      state.currentSongs = [...data]
    },
    // 播放当前歌单
    playnowMusiclist(state, data) {
      if (state.currentSongs.length != 0) {
        state.playmusicList = state.currentSongs
        // 选择播放全部歌单的同时自动播放第一首歌
        this.dispatch("getMusicDetail", state.playmusicList[0])
        // 检验歌单是否重复添加
        state.playlistIDs = []
        state.playlistIDs.push(data)
        setItem(playlistIDs, state.playlistIDs)
      } else if (state.currentSongs.length == 0) {
        console.log("数组还没加载")
      }
      setItem(playmusicList, state.playmusicList)
    },
    // 设置当前歌曲
    setcurrentMusic(state, data) {
      state.currentMusic = data
      setItem(currentMusic, state.currentMusic)
    },
    // 设置当前歌曲url
    setcurrentMusicurl(state, data) {
      state.currentMusicUrl = data
      setItem(currentMusicUrl, state.currentMusicUrl)
    },
    //获取当前歌曲index
    getcurrentIndex(state, data) {
      state.currentIndex = state.playmusicList.findIndex((item) => {
        return item.id == data.id
      })
      setItem(currentIndex, state.currentIndex)
    },
    setCurrentIndex(state, data) {
      // 后退键
      if (data == "back") {
        if (state.currentIndex == 0) {
          state.currentIndex = state.playmusicList.length - 1
          this.dispatch(
            "getMusicDetail",
            state.playmusicList[state.currentIndex]
          )
        } else {
          state.currentIndex = state.currentIndex - 1
          this.dispatch(
            "getMusicDetail",
            state.playmusicList[state.currentIndex]
          )
        }
      }
      // 前进键
      else if (data == "next") {
        if (state.currentIndex == state.playmusicList.length - 1) {
          state.currentIndex = 0
          this.dispatch(
            "getMusicDetail",
            state.playmusicList[state.currentIndex]
          )
        } else {
          state.currentIndex = state.currentIndex + 1
          this.dispatch(
            "getMusicDetail",
            state.playmusicList[state.currentIndex]
          )
        }
      }
    },
    // 清除播放音乐与歌单
    clearAll(state) {
      state.currentIndex = null
      state.currentMusic = null
      state.currentMusicUrl = null
      state.playmusicList = []
      state.playlistIDs = []
      setItem(playlistIDs, state.playlistIDs)
      setItem(currentMusic, state.currentMusic)
      setItem(currentIndex, state.currentIndex),
        setItem(currentMusicUrl, state.currentMusicUrl)
      setItem(playmusicList, state.playmusicList)
    },
    // 添加当前歌曲进入歌曲队列(搜索栏添加的歌曲)
    addMusictoMusiclistbySearch(state, data) {
      let newlist = state.playmusicList.filter((music) => music !== data)
      newlist.push(data)
      state.playmusicList = newlist
    },
    // 播放新音乐，清空列表，添加新音乐列表
    addMusictoMuiscListbyNewmusic(state, data) {
      state.playmusicList = [...data]
      setItem(playmusicList, state.playmusicList)
    },
  },
  actions: {
    // 获取音乐详情  传入的为歌曲详情对象
    async getMusicDetail(context, musicdata) {
      const musicurl = await getmusicurlAPI(musicdata.id)
      context.commit("setcurrentMusic", musicdata)
      context.commit("setcurrentMusicurl", musicurl.data)
    },
  },
  modules: {
    A: moduleA,
  },
})
export default store
